package service.array;

import dto.Userdto;
import entity.Dept;
import entity.Role;
import entity.User;
import service.IDeptService;
import service.IRoleService;
import service.IUserService;
import vo.Result;
import vo.Uservo;


public class UserService extends BaseService<User, Userdto, Uservo> implements IUserService {

    private final IDeptService deptService;
    private final IRoleService roleService;

    private static UserService singleton;

    private UserService() {
        deptService = DeptService.getInstance();
        roleService = RoleService.getInstance();
        data = new User[DATA_SIZE + 1];
        temp = new Uservo[DATA_SIZE];
    }

    public static IUserService getInstance() {
        if (singleton == null) {
            singleton = new UserService();
        }
        return singleton;
    }

    @Override
    Uservo convertToVo(User user) {
        String deptName = "";
        Dept dept=deptService.findById(user.deptId);
        if (dept != null){
            deptName = dept.name;
        }
        String roleName = "";
        Role role=roleService.findById(user.roleId);
        if (role != null){
            roleName = role.name;
        }
        return new Uservo(user.getId(), user.username, user.name, user.mobile, user.email, user.deptId,
                deptName, user.roleId, roleName,user.enabled);
    }

    @Override
    User createEntity(Userdto dto, int id) {
        return new User(id,dto.roleId,dto.name,dto.mobile,dto.email,dto.deptId, dto.username);
    }

    @Override
    void updateEntity(Userdto dto, User user) {
        user.username  = dto.username;
        user.name  = dto.name;
        user.mobile = dto.mobile;
        user.email = dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }

    @Override
    public boolean deptInUse(int deptId) {
        boolean  inUse = false;
        for (User user : data){
            if (user!=null && user.deptId==deptId){
                inUse = true;
                break;
            }
        }
        return inUse;
    }

    @Override
    public boolean roleInUse(int roleId) {
        boolean  inUse = false;
        for (User user : data){
            if (user!=null && user.deptId==roleId){
                inUse = true;
                break;
            }
        }
        return inUse;
    }

    @Override
    public Result create(Userdto dto) {
        if (!deptService.existById(dto.deptId)) {
            return Result.failure("创建失败，不存在指定部门");
        }
        if (!roleService.existById(dto.roleId)) {
            return Result.failure("创建失败，不存在指定角色");
        }
        return super.create(dto);
    }

    @Override
    public Result update(Userdto dto,int id) {
        if (!deptService.existById(dto.deptId)) {
            return Result.failure("创建失败，不存在指定部门");
        }
        if (!roleService.existById(dto.roleId)) {
            return Result.failure("创建失败，不存在指定角色");
        }
        return super.update(dto,id);
    }

    @Override
    public  Result toggle(int id) {
        if(!existById(id)){
            return Result.failure("切换状态失败，不存在该数据");
        }
        data[id].enabled = !data[id].enabled;
        return Result.success(data[id].enabled ? "启用成功" : "禁用成功", null);
    }
}
